package main

import (
	"fmt"
	"time"
)

func main() {
	formatTimeStr := "2017-04-11 23:59:59"

	formatTime, err := time.Parse("2006-01-02 15:04:05", formatTimeStr)
	if err == nil {
		fmt.Println(formatTime) //打印结果：2017-04-11 13:33:37 +0000 UTC
	}

	//d, _ := time.ParseDuration("-24h")
	//d1, _ := time.ParseDuration("+1s")
	//formatTime = formatTime.Add(d).Add(d1)

	//cstLocal, _ := time.LoadLocation("Asia/Shanghai")
	d2, _ := time.ParseDuration("-23h59m59s")
	formatTime = formatTime.Add(d2)

	fmt.Println("formatTime1=", formatTime) //formatTime1= 2017-04-11 00:00:00 +0000 UTC

	r1 := formatTime.Format("2006-01-02")
	fmt.Println(r1)

	t, _ := time.Parse("2006-01-02 15:04:05", "2020-12-05 23:59:59")
	r := GetCacheExpireTtl(t)
	fmt.Println(r)

	r2 := a1()
	fmt.Println(r2)

	err = a2()
	if err != nil {
		fmt.Println("err,", err)
	}
}

func GetCacheExpireTtl(useEndTime time.Time) (res int) {
	endTimeStamp := useEndTime.Unix()
	nowTimeStamp := time.Now().Unix()

	if nowTimeStamp >= endTimeStamp {
		return 0
	}
	return int(endTimeStamp - nowTimeStamp)
}

func a1() (res *[]int) {
	return
}

func a2() error {
	err := a3()
	if err != nil {
		return fmt.Errorf("a2 err %v", err)
	}
	return nil
}

func a3() error {
	return fmt.Errorf("foo")
}

func getDurationDay() int64 {
	endStr := time.Now().Format("2006-01-02")
	endTime, _ := time.Parse("2006-01-02", endStr)
	end := endTime.Unix()

	startTime, _ := time.Parse("2006-01-02", "2020-01-2")
	start := startTime.Unix()

	d := (end-start)/86400 + 1
	return d
}
